<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-06-06
 * Time: 15:37
 */
namespace app\admin\controller;

use think\Db;
use think\Exception;
use think\Request;

class Member extends Base
{

    protected static $err_code = 0;

    /**
     * 空操作
     */
    public function _empty(){
        if($this->check_admin_login()){
            $this->redirect('admin/Index/index');
        }else{
            $this->redirect('admin/login');
        }
    }

    /**
     * 用户列表
     */
    public function member_list(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);

        $gets['phone'] = request()->get('phone','');//手机号
        $gets['nickname'] = request()->get('nickname','');//昵称
        $gets['tb_band'] = request()->get('tb_band',0);//绑定状态
        $gets['status'] = request()->get('status',1);//是否禁用
        if (!empty($gets['phone'])){
            $where['phone'] = $gets['phone'];
        }
        if (!empty($gets['phone'])){
            $where['phone'] = $gets['phone'];
        }
        if (!empty($gets['nickname'])){
            $where['nickname'] = ['like',"%".$gets['phone']."%"];
        }
        $where['tb_band'] = $gets['tb_band'];
        $where['status'] = $gets['status'];
        $member_list = Db::name('member')->field('id,phone,last_login_time,status,tb_band,headimgurl,nickname,reg_time')->where($where)->order('id desc')->paginate(10, false, ['query' => $page_param]);
        //分页信息
        $page = $member_list->render();
        $member_list = $member_list->toArray();
        if (!empty($member_list['data'])){
            foreach ($member_list['data'] as $k=>$v){
                $member_list['data'][$k]['headimgurl'] = get_img_url($v['headimgurl'],'headimgurl');
            }
        }
        //模板变量赋值
        $this->assign('gets', $gets);
        $this->assign('list', $member_list['data']);
        $this->assign('total', $member_list['total']);
        $this->assign('page', $page);

        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 用户添加
     */
    public function member_add(){
        if ($this->request->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                if (empty($data)){
                    throw new Exception('参数不能为空');
                }
                // 验证参数
                $validate_member = new \app\admin\validate\Member;
                if (!$validate_member->scene('member_add')->check($data))
                {
                    throw new Exception($validate_member->getError());
                }
                $flag = Db::name('member')->insert([
                    'phone'=>$data['phone'],
                    'password'=> md5(md5($data['password']) . md5($data['phone'])), //密码
                    'nickname'=>$data['nickname'],
                    'reg_ip'=>getIPaddress(),
                    'reg_time'=>time(),
                    'reg_source'=>'web',
                    'headimgurl'=>$data['headimgurl'],
                    'sex'=>$data['sex'],
                ]);
                if ($flag!= false){
                    Db::commit();
                    return json(['code'=>200,'msg'=>'success']);
                }
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 用户编辑
     */
    public function member_edit(){

        if ($this->request->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                if (empty($data)){
                    throw new Exception('参数不能为空');
                }
                // 验证参数
                $validate_member = new \app\admin\validate\Member;
                if (!$validate_member->scene('member_edit')->check($data))
                {
                    throw new Exception($validate_member->getError());
                }
                if (!empty($data['password'])){
                    $data['password'] = md5(md5($data['password']) . md5($data['phone']));
                }
                $info = Db::name('member')->where('id',$data['id'])->find();
                if (empty($info['phone'])){
                    if (!empty($data['phone']) && empty($data['password'])){
                        throw new Exception('密码不能为空');
                    }elseif (empty($data['phone']) && !empty($data['password'])){
                        $update_data = [ 'id'=>$data['id'], 'nickname'=>$data['nickname'],'headimgurl'=>$data['headimgurl'],'sex'=>$data['sex']];
                    }elseif (empty($data['phone']) && empty($data['password'])){
                        $update_data = [ 'id'=>$data['id'], 'nickname'=>$data['nickname'],'headimgurl'=>$data['headimgurl'],'sex'=>$data['sex']];
                    }else{
                        $update_data = [ 'id'=>$data['id'], 'phone'=>$data['phone'],'password'=> $data['password'],'nickname'=>$data['nickname'],'headimgurl'=>$data['headimgurl'],'sex'=>$data['sex']];
                    }
                }else{
                    if (!empty($data['password'])){
                        $update_data = [ 'id'=>$data['id'],'phone'=>$data['phone'],'password'=> $data['password'],'nickname'=>$data['nickname'],'headimgurl'=>$data['headimgurl'],'sex'=>$data['sex']];
                    }else{
                        $update_data = [ 'id'=>$data['id'],'phone'=>$data['phone'],'nickname'=>$data['nickname'],'headimgurl'=>$data['headimgurl'],'sex'=>$data['sex']];
                    }

                }
                $flag = Db::name('member')->update($update_data);
                if ($flag){
                    Db::commit();
                    return json(['code'=>200,'msg'=>'success']);
                }
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            try{
                $member_id = input('param.id',0);
                if ($member_id == 0){
                    return json(['code'=>0,'msg'=>'用户id缺失']);
                }
                $member_info = Db::name('member')->where('id',$member_id)->find();
                if (empty($member_id)){
                    return json(['code'=>0,'msg'=>'用户id缺失']);
                }
                $member_info['host_headimgurl'] = get_img_url($member_info['headimgurl'],'headimgurl');
                $this->assign('member_info',$member_info);
            }catch (Exception $e){
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
            // 渲染模板输出
            return $this->fetch();
        }
    }

    //会员删除
    public function member_del(){
        $member_id=input('id','','intval');
        Db::startTrans();
        try{
            if (!is_numeric($member_id)){
                throw new Exception('会员id错误');
            }
            $rst=Db::name('member')->where('id',$member_id)->delete();
            if($rst!==false){
                Db::commit();
                return json(['code'=>200,'msg'=>'会员删除成功']);
            }else{
                throw new Exception('会员删除失败');
            }
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>0,'msg'=>$e->getMessage()]);
        }
    }
}